$('.box').on('click','a',function () {
    $(this).parents('.box').hide().siblings('.box').show();
});

$(".register form").on('submit',function (e) {
    e.preventDefault();
    let data = $(this).serialize();
    axios.post('/api/reguser',data)
    .then(res => {
        console.log(res)
        if(res.data.status === 0){
            layer.msg(res.data.message);
            $(".register form")[0].reset(); 
            $('.box a').trigger('click');
        }
       
    })
})


$('.login form').on('submit',function(e){
    e.preventDefault();
    let data = $(this).serialize();
    axios.post("/api/login",data)
    .then(res => {
        console.log(res)
        if(res.data.status===0){
            layer.msg(res.data.message,{time:500},function(){
                localStorage.setItem('token',res.data.token)
                location.href = './index.html'
            });
        }
        
    })
    
})

let form = layui.form;
form.verify({
    user:[/\w{2,10}/,'账号长度为2~10位'],
    pass:[/\S{2,12}/,'密码为2~12位且无空格'],
    same:function (val) {
        // val为形式参数  return的值为不通过的提示
        if(val!==$('.pwd').val()){
            return '两次密码不一致';
        }

    }
})

